//
//  FactoryOrderingViewController.m
//  quickBatchManager
//
//  Created by jack on 15/10/10.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "FactoryOrderingViewController.h"

@interface FactoryOrderingViewController ()<UITextFieldDelegate>
{
    UIView *maintableView;
    UIButton *singelBtn;
    UIButton *mulBtn;
    UILabel *totalLabel;
    NSMutableDictionary *selectNumber;
    NSMutableArray *colorList;
    NSMutableArray *sizeList;
    UIButton *addBtn;
    UIButton *exitBtn;
    ProductModel *productInfo;
    BOOL isAdd;
    
}
@end

@implementation FactoryOrderingViewController
@synthesize bgView,closeBtn,paramsModelNo,colorSizeView;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

-(void)initUI
{
    self.view.backgroundColor=[UIColor clearColor];
    UIImage *image = bgView.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    bgView.image=image;
    isAdd = NO;
    [closeBtn bk_whenTapped:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    singelBtn=[[UIButton alloc] initWithFrame:CGRectMake(marginSize, 10, 110, 40)];
    [singelBtn setTitle:@"单选" forState:UIControlStateNormal];
    
    [singelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [singelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    
    [singelBtn setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithRed:0.89 green:0.86 blue:0.84 alpha:1]] forState:UIControlStateNormal];
    [singelBtn setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithRed:0.06 green:0.64 blue:0.78 alpha:1]] forState:UIControlStateSelected];
    [singelBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    
    
    
    
    mulBtn=[[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(singelBtn.frame), CGRectGetMinY(singelBtn.frame), CGRectGetWidth(singelBtn.frame), CGRectGetHeight(singelBtn.frame))];
    [mulBtn setTitle:@"多选" forState:UIControlStateNormal];
    
    [mulBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [mulBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    
    [mulBtn setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithRed:0.89 green:0.86 blue:0.84 alpha:1]] forState:UIControlStateNormal];
    [mulBtn setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithRed:0.06 green:0.64 blue:0.78 alpha:1]] forState:UIControlStateSelected];
    [mulBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    
    
    if(_isEdit)
    {
        [singelBtn setHidden:YES];
        [mulBtn setHidden: YES];
    }
    [singelBtn bk_whenTapped:^{
        singelBtn.selected=YES;
        mulBtn.selected=NO;
        
        for (NSString *key in selectNumber) {
            NumberFieldModel *number=[selectNumber objectForKey:key];
            number.numberField.backgroundColor=[UIColor whiteColor];
            number.isSelect=NO;
        }
        
    }];
    
    [mulBtn bk_whenTapped:^{
        singelBtn.selected=NO;
        mulBtn.selected=YES;
    }];
    
    singelBtn.selected=YES;
    
    [colorSizeView addSubview:singelBtn];
    [colorSizeView addSubview:mulBtn];
    
    
    maintableView=[[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(singelBtn.frame), CGRectGetMaxY(singelBtn.frame)+5, 480, 100)];
    
    [colorSizeView addSubview:maintableView];
    maintableView.backgroundColor=[UIColor colorWithRed:0.73 green:0.72 blue:0.69 alpha:1];
    
    
    //[self reloadNumber];
    
    /*** 添加关闭按钮***/
    addBtn = [[UIButton alloc] initWithFrame:CGRectMake(maintableView.frame.size.width/2 - 100, CGRectGetMaxY(maintableView.frame)+marginSize, 110, 40)];
    [addBtn setTitle:@"添加" forState:UIControlStateNormal];
    [addBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"btnred"] forState:UIControlStateNormal];
    
    exitBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addBtn.frame)+marginSize, CGRectGetMaxY(maintableView.frame)+marginSize, 110, 40)];
    [exitBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [exitBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [exitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [exitBtn setBackgroundImage:[UIImage imageNamed:@"btnred"] forState:UIControlStateNormal];
    
    [colorSizeView addSubview:addBtn];
    [colorSizeView addSubview:exitBtn];
    
    productInfo=[AppService getProductByNo:paramsModelNo];
    
    [self ChooseProduct:productInfo];
    
    [addBtn bk_whenTapped:^{
        NSLog(@"click----");
        
        if(!isAdd)
        {
            [AppService putInFactoryProduct:productInfo.productId andColorList:colorList andSizeList:sizeList andNumberList:selectNumber];
            isAdd = YES;
            if(_delegate!=nil && [_delegate respondsToSelector:@selector(factoryOrderingClose:)])
            {
                [_delegate factoryOrderingClose:self];
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    }];
    
    [closeBtn bk_whenTapped:^{
        if(_delegate!=nil && [_delegate respondsToSelector:@selector(factoryOrderingClose:)])
        {
            [_delegate factoryOrderingClose:self];
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [exitBtn bk_whenTapped:^{
        NSLog(@"click----exitBtn");
        if(_delegate!=nil && [_delegate respondsToSelector:@selector(factoryOrderingClose:)])
        {
            [_delegate factoryOrderingClose:self];
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
 
    
    if(_isEdit)
    {
        addBtn.hidden = YES;
        [exitBtn setFrame:CGRectMake(CGRectGetMaxX(addBtn.frame)-50,  CGRectGetMaxY(maintableView.frame)+marginSize, 110, 40)];
    }else
    {
         addBtn.hidden = NO;
    }
}


-(void)initData
{
    selectNumber=[[NSMutableDictionary alloc] init];
    colorList = [[NSMutableArray alloc] init];
    sizeList = [[NSMutableArray alloc] init];
    
}



-(void)textFieldChanged:(id)sender
{
    UITextField *text=(UITextField*)sender;
    double total=0;
    for (NSString *key in selectNumber) {
        NumberFieldModel *number=[selectNumber objectForKey:key];
        if(number.isSelect && mulBtn.isSelected)
        {
            number.numberField.text=text.text;
        }
        
        total+=[number.numberField.text intValue];
    }
    
    totalLabel.text=[NSString stringWithFormat:@"    共计：%.0f件",total];
    
}


-(void) reloadNumber
{
    
    for (UIView *view in maintableView.subviews) {
        [view removeFromSuperview];
    }
    
    if(sizeList.count>0 && colorList.count>0)
    {
        colorSizeView.hidden=NO;
        int width=580/(sizeList.count+1);
        int height=40;
        
        for (int i=0; i<=colorList.count; i++) {
            for (int j=0; j<=sizeList.count; j++) {
                NSString *key=[NSString stringWithFormat:@"%d_%d",i,j];
                if(i>0 && j>0)
                {
                    
                    NumberFieldModel *number=[selectNumber objectForKey:key];
                    
                    UITextField *numberField=[[UITextField alloc] initWithFrame:CGRectMake((width+1)*j+1,(height+1)*i+1, width, height)];
                    numberField.backgroundColor=[UIColor whiteColor];
                    numberField.textAlignment=NSTextAlignmentCenter;
                    numberField.delegate=self;
                    if(_isEdit)
                    {
                        [numberField setEnabled:NO];
                        FactoryProductInfoModel *model = [FactoryProductInfoModel searchSingleWithWhere:[NSString stringWithFormat:@"date='%@' and sizeName = '%@' and colorName='%@'",_date,sizeList[j-1],colorList[i-1]] orderBy:nil];
                        numberField.text = [NSString stringWithFormat:@"%d",model.factoryProductNumber];
                    }
                    numberField.font=[UIFont systemFontOfSize:16];
                    numberField.textColor=[UIColor colorWithRed:0.50 green:0.49 blue:0.46 alpha:1];
                    numberField.keyboardType=UIKeyboardTypeNumberPad;
                    number.numberField=numberField;
                    [self addTextFieldChange:numberField];
                    
                    
                    UIView *view=[[UIView alloc] initWithFrame:numberField.frame];
                    
                    view.userInteractionEnabled=YES;
                    
                    [view bk_whenTapped:^{

                        if(mulBtn.selected)
                        {
                            number.isSelect=!number.isSelect;
                            if(!number.isSelect)
                            {
                                numberField.backgroundColor=[UIColor whiteColor];
                                [numberField resignFirstResponder];
                            }
                            else {
                                numberField.backgroundColor=[UIColor colorWithWhite:0.95 alpha:1];
                                [numberField becomeFirstResponder];
                            }
                            
                        }
                        else
                        {
                            [numberField becomeFirstResponder];
                        }
                        
                    }];
                    
                    
                    [maintableView addSubview:numberField];
                    [maintableView addSubview:view];
                }
                else
                {
                    
                    UILabel *title=[[UILabel alloc] initWithFrame:CGRectMake((width+1)*j+1,(height+1)*i+1, width, height)];
                    if(i==0)
                        title.backgroundColor=[UIColor colorWithRed:0.97 green:0.96 blue:0.93 alpha:1];
                    else title.backgroundColor=[UIColor whiteColor];
                    
                    title.textAlignment=NSTextAlignmentCenter;
                    title.font=[UIFont systemFontOfSize:16];
                    title.textColor=[UIColor colorWithRed:0.50 green:0.49 blue:0.46 alpha:1];
                    
                    
                    if(i==0 && j==0)
                        title.text=@"颜色";
                    else if(j==0)
                    {
                        title.text=colorList[i-1];
                    }
                    else if(i==0)
                    {
                        title.text=sizeList[j-1];
                    }
                    
                    [maintableView addSubview:title];
                }
                
                
            }
            
            
        }
        
        maintableView.frame=CGRectMake(maintableView.frame.origin.x,maintableView.frame.origin.y, (width+1)*(sizeList.count+1)+1, (height+1)*(colorList.count+2)+1);
        
        totalLabel=[[UILabel alloc] initWithFrame:CGRectMake(1,CGRectGetHeight(maintableView.frame)-1-height, CGRectGetWidth(maintableView.frame)-2, height)];
        totalLabel.backgroundColor=[UIColor whiteColor];
        
        totalLabel.textAlignment=NSTextAlignmentLeft;
        totalLabel.font=[UIFont systemFontOfSize:16];
        totalLabel.textColor=[UIColor colorWithRed:0.50 green:0.49 blue:0.46 alpha:1];
        totalLabel.text=[NSString stringWithFormat:@"    共计：%d件",0];
        if(_isEdit)
        {
            double number = [FactoryProductInfoModel sumWithWhere:@"factoryProductNumber" where:[NSString stringWithFormat:@"date='%@'",_date]];
            totalLabel.text=[NSString stringWithFormat:@"    共计：%0.0f件",number];
        }
        
        [maintableView addSubview:totalLabel];
        
        for (NSLayoutConstraint *ct in  [colorSizeView constraints]) {
            if(ct.firstAttribute==NSLayoutAttributeHeight)
            {
                ct.constant=CGRectGetMaxY(maintableView.frame)+10;
            }
        }
        
        [addBtn setFrame:CGRectMake(maintableView.frame.size.width/2 - 100, CGRectGetMaxY(maintableView.frame)+marginSize, 110, 40)];
        [exitBtn setFrame:CGRectMake(CGRectGetMaxX(addBtn.frame)+marginSize, CGRectGetMaxY(maintableView.frame)+marginSize, 110, 40)];
        
        
    }
}


-(void)ChooseProduct:(ProductModel *)product
{
    [colorList removeAllObjects];
    [sizeList removeAllObjects];
    [selectNumber removeAllObjects];
    //查询产品信息,尺寸颜色表
    
    NSDictionary *sizeColor=[AppService getSizeAndColorByModel:product.modelNo];
    colorList=[sizeColor objectForKey:@"color"];
    sizeList=[sizeColor objectForKey:@"size"];
    
    if(sizeList.count>0 && colorList.count>0)
    {
        for (int i=1; i<=colorList.count; i++) {
            for (int j=1; j<=sizeList.count; j++) {
                
                NumberFieldModel *number=[[NumberFieldModel alloc] init];
                number.isSelect=NO;
                number.number=0;
                [selectNumber setObject:number forKey:[NSString stringWithFormat:@"%d_%d",i,j]];
            }
        }
        
        [self reloadNumber];
        
    }
    
    
    
}


@end
